<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User extends MY_Controller {
    
    private $user;
	public function __construct(){
		parent::__construct();
	    $user = $this->cm->get_by_id($this->tbl->member,$this->loginer->id);
	    $this->user = $user;
	    $this->_data['user'] = $user;
	    
	    $this->load->model('member_model','member');
	    
	}

	function setting_basic(){

	    $this->check_page_login();
	    $this->_data['sn_basic'] = ' csn ';
	    $this->load->view('user/setting_basic',$this->_data);
	    
	}
	
	function setting_avatar(){
	    $this->check_page_login();
	    
	    $this->_data['sn_avatar'] = ' csn ';
	    $this->load->view('user/setting_avatar',$this->_data);
	    
	}
	
	function setting_pass(){
	    $this->check_page_login();
	    
	    $this->_data['sn_pass'] = ' csn ';
	    $this->load->view('user/setting_pass',$this->_data);
	    
	}
	
	function save_do(){
	    $this->check_ajax_login();
	    $this->p['id'] = $this->loginer->id;
	    $this->cm->save($this->tbl->member,$this->p);
	    
	    ejd('','','reload');
	    
	}
	
	function setting_pass_do(){
	    $this->check_ajax_login();
	    
	    if(empty($this->p['opass'])) die;
	    if(empty($this->p['upass'])) die;
	    $opass = md5($this->p['opass']);
	    $upass = md5($this->p['upass']);
	    
	    //旧密码是否正确
	    if(!$this->cm->in($this->tbl->member,array('id'=>$this->loginer->id,'upass'=>$opass)))
	        ejd('','旧密码不正确！');
	        
	    //保存密码
	    $this->cm->save($this->tbl->member,array('id'=>$this->loginer->id,'upass'=>$upass));
	    
	    ejd('','操作成功！','reload');
	    
	}

	function fans(){
	    $this->check_page_login();
	    
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
	    $list_fans = $this->member->get_fans_by_page($limit_num,$this->loginer->id);
	    
	    //分页导航
	    $this->pagination->initialize(config_item('pagefollow')); 
    	$this->pagination->base_url = $this->page_url();;
	    $this->pagination->total_rows = $this->member->count;
	    $this->_data['pagenav'] = $this->pagination->create_links();

	    $this->_data['list_fans'] = $list_fans;
	    $this->_data['fn_fans'] = ' csn ';

	    
	    $this->load->view('user/fans',$this->_data);
	    
    }

	function follow(){
	    $this->check_page_login();
	    
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
	    $list_follow = $this->member->get_follow_by_page($limit_num,$this->loginer->id);
	    
	    //分页导航
	    $this->pagination->initialize(config_item('pagefollow')); 
    	$this->pagination->base_url = $this->page_url();;
	    $this->pagination->total_rows = $this->member->count;
	    $this->_data['pagenav'] = $this->pagination->create_links();

	    $this->_data['list_follow'] = $list_follow;
	    $this->_data['fn_follow'] = ' csn ';

	    
	    $this->load->view('user/follow',$this->_data);
	    
	}
	
	function friend(){
	    $this->check_page_login();
	    
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
	    $list_friend = $this->member->get_friend_by_page($limit_num,$this->loginer->id);
	    //分页导航
	    $this->pagination->initialize(config_item('pagefollow')); 
    	$this->pagination->base_url = $this->page_url();;
	    $this->pagination->total_rows = $this->member->count;
	    $this->_data['pagenav'] = $this->pagination->create_links();

	    $this->_data['list_friend'] = $list_friend;
	    $this->_data['fn_friend'] = ' csn ';
	    
	    $this->load->view('user/friend',$this->_data);
	}
	
	function find(){

	    $this->check_page_login();
	    
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
	    $list = $this->member->get_by_page($limit_num,$this->p,$this->loginer->id);
	    
	    //分页导航
	    $this->pagination->initialize(config_item('pagefollow')); 
    	$this->pagination->base_url = $this->page_url();;
	    $this->pagination->total_rows = $this->member->count;
	    $this->_data['pagenav'] = $this->pagination->create_links();

	    $this->_data['list'] = $list;
	    $this->_data['fn_find'] = ' csn ';
	    $this->_data['p'] = $this->p;
	    $this->load->view('user/find',$this->_data);
	    
	}
	
	function invit(){
	    $this->check_page_login();
	    $this->_data['fn_invit'] = ' csn ';
	    $this->load->view('user/invit',$this->_data);
	}
	
	function unfollow(){
	    $this->check_ajax_login();
	    if(empty($this->p['id'])) die;
	    $id = $this->p['id'];
	    $this->cm->delete($this->tbl->follow,array('uid'=>$this->loginer->id,'fid'=>$id));
	    
	    ejd('','','reload');
	    
	}
	
	
	function follow_do(){
		
		//登录检测
		$this->check_ajax_login();
		
		//数据校验
		$id = $this->p['id'];
		if(empty($id)) die;
		if(!$this->cm->in('member',array('id'=>$id))) die;

		$cond = array('uid'=>$this->loginer->id,'fid'=>$id);
		$this->cm->insert('follow',$cond);
		echo ejd('','','reload');
	}
	
	
	function msg(){
	    $this->check_page_login();
	    
	    $this->load->library('pagination');
	    $limit_num = !empty($_GET['per_page']) ? intval($_GET['per_page']) : 0;
	    
        $this->load->model('msg_model','msg');	    
        $this->_data['list_msg'] = $this->msg->get_by_page($limit_num,$this->loginer->id);
        
	    //分页导航
    	$this->pagination->base_url = $this->page_url($this->p);;
	    $this->pagination->total_rows = $this->msg->count;
	    $this->_data['pagenav'] = $this->pagination->create_links();
	    
	    $this->load->view('user/msg',$this->_data);
	    
	}
}

